package com.dkd.applet.service.impl;

import cn.elegent.ac.ElegentAC;
import cn.elegent.pay.CallBackService;
import cn.elegent.pay.ElegentPay;
import cn.elegent.pay.dto.QueryResponse;
import cn.elegent.pay.dto.WatchDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dkd.applet.common.constant.VmSystemConstant;
import com.dkd.applet.common.core.ResponseResult;
import com.dkd.applet.common.exception.LogicException;
import com.dkd.applet.config.TopicConfig;

import com.dkd.applet.domain.Order;
import com.dkd.applet.domain.dto.VendoutDto;
import com.dkd.applet.service.ChannelService;
import com.dkd.applet.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 回调类
 */
@Service
@Slf4j
public class CallBackServiceImpl implements CallBackService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ElegentPay elegentPay;
    @Autowired
    private ElegentAC elegentAC;
    /**
     * 支付成功回调处理方法
     * @param orderSn 订单编号
     */
    @Override
    public void successPay(String orderSn) {
        log.error("支付成功回调！!!"+orderSn);
        //log.info("pay callback success,orderNo:{"+orderSn+"}");
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, orderSn));
        //查询订单
        QueryResponse queryResponse = elegentPay.queryTradingOrderNo(orderSn, order.getPayType());
        if(queryResponse==null){
            throw new LogicException("第三方订单不存在！");
        }
        if(order!=null){
            if(order.getStatus().equals(VmSystemConstant.ORDER_STATUS_CREATE)){
                order.setPayStatus(VmSystemConstant.PAY_STATUS_PAYED); //支付状态:支付完成
                order.setStatus(VmSystemConstant.ORDER_STATUS_PAYED); //订单状态:支付完成
                order.setTradeNo(queryResponse.getTransaction_id());
                orderService.updateById(order);
                elegentAC.publish(TopicConfig.PAY_SERVER_TOPIC, ResponseResult.success("支付成功"));
                //实现发货
                VendoutDto vendoutDto = new VendoutDto();
                vendoutDto.setInnerCode(order.getInnerCode());
                vendoutDto.setOrderNo(order.getOrderNo());
                vendoutDto.setChannelCode(order.getChannelCode());
                vendoutDto.setSkuId(order.getSkuId());
                elegentAC.publish(TopicConfig.getVendoutTopic(order.getInnerCode()), vendoutDto);
                //查询指定设备的指定货道
//                LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
//                queryWrapper.eq(Channel::getInnerCode,order.getInnerCode());
//                queryWrapper.eq(Channel::getChannelCode,order.getChannelCode());
//                Channel channel = channelService.getOne(queryWrapper);
//                if(channel!=null){
//                    //出货成功过将货道中的商品数量减1
//                    channel.setCurrentCapacity(channel.getCurrentCapacity()-1);
//                    channelService.updateById(channel);
//                }else{
//                    throw new LogicException("设备或货道不存在！");
//                }
//
            }else{
                throw new LogicException("订单状态异常！");
            }
        }else{
            throw new LogicException("订单不存在！");
        }
    }

    /**
     * 支付失败回调处理方法
     * @param orderSn 订单编号
     */
    @Override
    public void failPay(String orderSn) {
        log.error("支付失败回调！!!!"+orderSn);
        //log.info("pay callback fail,orderNo:{"+orderSn+"}");
    }

    /**
     * 退款成功回调处理方法
     * @param orderSn 订单编号
     */
    @Override
    public void successRefund(String orderSn) {
        log.error("退款成功回调！!!!"+orderSn);
        //log.info("refund callback success,orderNo:{"+orderSn+"}");
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, orderSn));
        if(order!=null){
            order.setPayStatus(VmSystemConstant.PAY_STATUS_REFUNDIED);//退款完成
            //如果订单状态为支付完成，说明订单还未出货就被退款，直接订单失效
            if(order.getStatus().equals(VmSystemConstant.ORDER_STATUS_PAYED)){
                order.setStatus(VmSystemConstant.ORDER_STATUS_INVALID); //订单状态:失效
                order.setCancelDesc("订单退款成功");
            }
            orderService.updateById(order);
        }
    }

    /**
     * 退款失败回调处理方法
     * @param orderSn 订单编号
     */
    @Override
    public void failRefund(String orderSn) {
        log.error("退款失败回调！!!!"+orderSn);
        //log.info("refund callback fail,orderNo:{"+orderSn+"}");
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, orderSn));
        if(order!=null){
            order.setPayStatus(VmSystemConstant.PAY_STATUS_REFUNDING); //支付状态:退款中
            order.setStatus(VmSystemConstant.ORDER_STATUS_PAYED); //订单状态:支付完成
            orderService.updateById(order);
        }
    }

}
